<?php

/**
 * Enter description here ...
 * @author Kevin
 * @version 1.0
 * @created 12-d�c.-2011 19:51:40
 */
class Bet implements I_Persistent
{

	var $event = NULL;
	var $exist = false;
	var $id = NULL;
	var $option = NULL;
	var $quote = NULL;
	var $tip = NULL;
	var $type = NULL;
	var $winning = NULL;

	/**
	 * Bet
	 * @param int $pId
	 * 
	 * @param pId
	 */
	function Bet($pId)
	{
		global $wpdb;
		$_row = $wpdb->get_row($wpdb->prepare("SELECT * FROM bets WHERE id = %d", $pId));
		
		$this->id = $pId;
		if($_row->id > 0){
			$this->event = $_row->fk_event;
			$this->type = $_row->fk_bettype;
			$this->tip = $_row->tip;
			$this->option = $_row->option;
			$this->quote = $_row->quote;
			$this->winning = $_row->winning;
				
			$this->exist = true;
		}
	}

	/**
	 * Enter description here ...
	 */
	function create()
	{
		global $wpdb;
			
		$queryTable = 'bets';
		$queryData = array(
					'id' => $this->id, 
					'fk_event' => $this->event,
					'fk_bettype' => $this->type,
					'tip' => $this->tip,
					'option' => $this->option,
					'quote' => $this->quote,
					'winning' => 0
		);
		$queryDataFormat = array('%d','%d','%d','%s','%s','%f','%d');
		
		$wpdb->insert( $queryTable, $queryData, $queryDataFormat);
	}

	
	/**
	* Enter description here ...
	*/
	function save()
	{
	global $wpdb;
		
	$queryTable = 'bets';
	$queryData = array(
	'fk_event' => $this->event,
	'fk_bettype' => $this->type,
						'tip' => $this->tip,
						'option' => $this->option,
	'quote' => $this->quote,
						'winning' => $this->winning
			);
			$queryDataFormat = array('%d','%d','%s','%s','%f','%d');
			$queryCondition = array("id" => $this->id);
	$queryConditionFormat = array('%d');
	
	$wpdb->update( $queryTable, $queryData, $queryCondition, $queryDataFormat, $queryConditionFormat);
	}
	
	
	function delete()
	{
	}


	public function setId($id)
	{
	    $this->id = $id;
	}
	
	public function getId()
	{
	    return $this->id;
	}

	public function setEvent($event)
	{
	    $this->event = $event;
	}
	
	public function getEvent()
	{
	    return $this->event;
	}
	
	public function setType($type)
	{
	    $this->type = $type;
	}
	
	public function getType()
	{
	    return $this->type;
	}
	
	function getTypeName()
	{
		global $wpdb;
	
		$brutName =
		$wpdb->get_var(
		$wpdb->prepare(
		// requete
							"SELECT name FROM bettypes WHERE id = %d",
		// parametres
		$this->type
		)
		);
	
		$translatedName = getTranslation("bets", $brutName);
	
		// remplacement des zone parametrable
		$_OPTION_ = $this->option;
		$_OPTIONINVERSE_ = - floatval($this->option);
	
		$initial = array("%OPTION%", "%OPTIONINVERSE%");
		$final   = array($_OPTION_, $_OPTIONINVERSE_);
		$return = str_replace($initial, $final, $translatedName);
	
		return $return;
	}
	
	public function setOption($option)
	{
	    $this->option = $option;
	}
	
	public function getOption()
	{
	    return $this->option;
	}

	public function setQuote($quote)
	{
	    $this->quote = $quote;
	}
	
	public function getQuote()
	{
	    return number_format($this->quote,2);
	}

	function setTip($pTip){
		$this->tip = $pTip;
	}
	
	function getTip()
	{
		$brutTip = $this->tip;
		
		if($this->type == 33) return $brutTip;	// score exact
		
		$translatedTip = getTranslation("bets", $brutTip);
		
		// remplacement des zone parametrable
		$_OPTION_ = $this->option;
		$_TEAM1_ = "1";
		$_TEAM2_ = "2";
		
		$initial = array("%TEAM1%", "%TEAM2%", "%OPTION%");
		$final   = array($_TEAM1_, $_TEAM2_, $_OPTION_);
		$return = str_replace($initial, $final, $translatedTip);
		
		return $return;
	}

	
	
	public function setWinning($winning)
	{
	    $this->winning = $winning;
	}
	
	public function getWinning()
	{
	    return $this->winning;
	}

	
	function updateWinning()
	{
	}
	
	public function setExist($exist)
	{
	    $this->exist = $exist;
	}
	
	public function getExist()
	{
	    return $this->exist;
	}


}
?>